package com.cy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cy.mapper.BookMapper;
import com.cy.model.Book;
import com.cy.service.IBookService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Objects;

@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements IBookService {

    @Override
    public IPage<Book> page(Integer currentPage, Integer pageSize, Book book) {
        if (Objects.isNull(currentPage) || currentPage < 1) {
            currentPage = 1;
        }
        if (Objects.isNull(pageSize) || pageSize < 1) {
            pageSize = 10;
        }
        // 构建分页对象
        IPage<Book> page = new Page<>(currentPage, pageSize);
        // 构建查询对象
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasLength(book.getName())) {
            queryWrapper.like("name", book.getName());
        }
        if (StringUtils.hasLength(book.getAuthor())) {
            queryWrapper.or().like("author", book.getAuthor());
        }
        return baseMapper.selectPage(page, queryWrapper);
    }

}
